package cn.iocoder.yudao.module.reading.convert.bookset;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.reading.controller.app.book.vo.PictureBookRespVO;
import cn.iocoder.yudao.module.reading.controller.app.bookset.vo.BookSetDetailRespVO;
import cn.iocoder.yudao.module.reading.controller.app.bookset.vo.BookSetRespVO;
import cn.iocoder.yudao.module.reading.dal.dataobject.book.PictureBookDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.bookset.BookSetDO;
import cn.iocoder.yudao.module.reading.dal.dataobject.record.ReadingRecordDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

/**
 * 绘本集 Convert
 *
 * @author 芋道源码
 */
@Mapper
public interface BookSetConvert {

    BookSetConvert INSTANCE = Mappers.getMapper(BookSetConvert.class);

    default BookSetRespVO convert(BookSetDO bean) {
        if (bean == null) {
            return null;
        }
        BookSetRespVO respVO = convertBasic(bean);
        respVO.convertUrls(); // 转换URL
        return respVO;
    }

    default List<BookSetRespVO> convertList(List<BookSetDO> list) {
        if (list == null) {
            return null;
        }
        return list.stream().map(this::convert).collect(java.util.stream.Collectors.toList());
    }

    default PageResult<BookSetRespVO> convertPage(PageResult<BookSetDO> page) {
        if (page == null) {
            return null;
        }
        PageResult<BookSetRespVO> result = new PageResult<>();
        result.setList(convertList(page.getList()));
        result.setTotal(page.getTotal());
        return result;
    }

    // 基础转换方法，不包含URL转换
    @Mapping(target = "id", source = "id")
    @Mapping(target = "title", source = "title")
    @Mapping(target = "cover", source = "cover")
    @Mapping(target = "description", source = "description")
    @Mapping(target = "categoryId", source = "categoryId")
    @Mapping(target = "ageRange", source = "ageRange")
    @Mapping(target = "bookCount", source = "bookCount")
    @Mapping(target = "source", source = "source")
    @Mapping(target = "difficultyLevel", source = "difficultyLevel")
    @Mapping(target = "language", source = "language")
    @Mapping(target = "isRecommended", source = "isRecommended")
    BookSetRespVO convertBasic(BookSetDO bean);

    @Mapping(target = "books", ignore = true) // books 字段需要单独设置
    BookSetDetailRespVO convertDetail(BookSetDO bean);

    /**
     * 转换绘本列表，包含阅读状态
     */
    default List<PictureBookRespVO> convertBookList(List<PictureBookDO> books,
                                                   Map<Long, ReadingRecordDO> readingRecords,
                                                   String bookSetTitle) {
        return books.stream().map(book -> {
            PictureBookRespVO respVO = new PictureBookRespVO();
            respVO.setId(book.getId());
            respVO.setTitle(book.getTitle());
            respVO.setCover(book.getCover());
            respVO.setPages(book.getPages());
            respVO.setSortOrder(book.getSortOrder());
            respVO.setBookSetId(book.getBookSetId());
            respVO.setBookSetTitle(bookSetTitle);

            // 设置阅读状态
            ReadingRecordDO record = readingRecords.get(book.getId());
            PictureBookRespVO.ReadStatusVO readStatus = new PictureBookRespVO.ReadStatusVO();
            if (record != null) {
                readStatus.setIsRead(record.getIsCompleted());
                readStatus.setReadCount(1); // 简化处理，实际可以统计次数
                readStatus.setLastReadAt(record.getLastReadAt() != null ? record.getLastReadAt().toString() : null);
                readStatus.setReadProgress(record.getReadingProgress());
            } else {
                readStatus.setIsRead(false);
                readStatus.setReadCount(0);
                readStatus.setLastReadAt(null);
                readStatus.setReadProgress(0.0);
            }
            respVO.setReadStatus(readStatus);

            // 转换URL
            respVO.convertUrls();

            return respVO;
        }).toList();
    }

}
